package com.symaster.common.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

/**
 * @author yinmiao
 * @date 2022-04-08 09:03
 */
public class JsonUtil {

    /**
     * 将b2所有键值对添加到b1
     */
    public static void composeTo(JSONObject b1, JSONObject b2) {
        if (b2 == null || b1 == null) {
            throw new NullPointerException();
        }

        for (String b2Key : b2.keySet()) {
            b1.put(b2Key, b2.get(b2Key));
        }
    }

    /**
     * 判断字符串是否为json格式
     */
    public static boolean isJsonStr(String str) {
        if (StringUtils.isEmpty(str)) {
            return false;
        }
        if (!str.startsWith("{") || !str.endsWith("}")) {
            return false;
        }

        try {
            JSONObject.parse(str);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static void main(String[] args) {
        System.out.println(isJsonStr("{\"keyTest\":\"valueTest\"}"));
    }

}
